package org.reliablesource.prism.gef.editor.parts;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;
import org.reliablesource.prism.gef.editor.commands.StickyNoteTextCommand;
import org.reliablesource.prism.gef.editor.figures.StickyNoteFigure;
import org.reliablesource.prism.gef.editor.model.StickyNoteAdapter;

/**
 * 
 * StickyNoteDirectEditPolicy.java 
 * Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved
 * 
 * Created on: Apr 12, 2006
 * 
 * @author Dennis Park <a
 *         href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 * 
 */

public class StickyNoteDirectEditPolicy extends DirectEditPolicy {

	/**
	 * @see DirectEditPolicy#getDirectEditCommand(DirectEditRequest)
	 */
	protected Command getDirectEditCommand(final DirectEditRequest edit) {
		String labelText = (String) edit.getCellEditor().getValue();
		StickyNoteEditPart label = (StickyNoteEditPart) getHost();
		StickyNoteTextCommand command = new StickyNoteTextCommand(
				(StickyNoteAdapter) label.getModel(), labelText, getHost()
						.getParent());
		return command;
	}

	/**
	 * @see DirectEditPolicy#showCurrentEditValue(DirectEditRequest)
	 */
	protected void showCurrentEditValue(final DirectEditRequest request) {
		String value = (String) request.getCellEditor().getValue();
		((StickyNoteFigure) getHostFigure()).setText(value);
		// hack to prevent async layout from placing the cell editor twice.
		getHostFigure().getUpdateManager().performUpdate();

	}

}
